package ema.tpjee.banking.controler;

import java.io.Serializable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import ema.tpjee.banking.dao.CustomerDao;
//import ema.tpjee.banking.dao.CustomerDaoImpl;
import ema.tpjee.banking.model.Bank;
import ema.tpjee.banking.model.Customer;
import ema.tpjee.banking.session.Session;

/**
 * Simple client of the stateless.
 * 
 * @author pfister
 */

/**
 * le contexte est maintenu sur le web tiers
 */
public final class CustomerControler implements Serializable {

	private static final long serialVersionUID = -7551732523128336257L;

	private CustomerDao customerDao = null;



	private Session session;

	public List<Customer> getCustomers() {
		if (session.getCurrentBank() == null)
			session
					.setCurrentBank(session.getBankControler().getBanks()
							.get(0));
		List<Customer> customers = customerDao
				.getList(session.getCurrentBank());
		return customers;
	}

	public void setCustomers(List<Customer> customers) {

	}

	private Customer getCurrentCustomer() {
		return session.getCurrentCustomer();
	}

	private void setCurrentCustomer(Customer currentCustomer) {
		session.setCurrentCustomer(currentCustomer);
	}

	public String add() {
		return "list";
	}

	public String find(int id) {
		setCurrentCustomer(customerDao.find(session.getCurrentBank(), id));
		return "edit";
	}

	public String delete() {
		setCurrentCustomer(customerDao.delete(session.getCurrentBank(),
				getCurrentCustomer()));
		return "list";
	}

	public String first() {
		setCurrentCustomer(customerDao.first(session.getCurrentBank()));
		return "edit";
	}

	public String last() {
		setCurrentCustomer(customerDao.last(session.getCurrentBank()));
		return "edit";
	}

	public String prior() {
		setCurrentCustomer(customerDao.prior(session.getCurrentBank(),
				getCurrentCustomer()));
		return "edit";
	}

	public String next() {
		setCurrentCustomer(customerDao.next(session.getCurrentBank(),
				getCurrentCustomer()));
		return "edit";
	}

	public String edit() {
		System.out.print("CustomerControler3.edit: ");
		System.out.println(getCurrentCustomer().getId());
		return "edit";
	}

	public String clone() {
		setCurrentCustomer(customerDao.clone(session.getCurrentBank(),
				getCurrentCustomer()));
		return "edit";
	}

	public String showCustomers() {
		System.out.println("action " + this.getClass().getName()
				+ " showCustomers()");
		return "customers";
	}

	public String create() {
		setCurrentCustomer(customerDao.create(session.getCurrentBank()));
		return "edit";
	}

	public String save() {
		setCurrentCustomer(customerDao.merge(session.getCurrentBank(),
				getCurrentCustomer()));
		return "list";
	}

	public String cancel() {
		System.out.println("cancel " + getCurrentCustomer().toString());
		return "list";
	}

	public String foobar() {
		setCurrentCustomer(customerDao.foobar(session.getCurrentBank(),
				getCurrentCustomer()));
		return "edit";
	}



	public String getDummy() {
		return test();
	}

	public void setDummy(String s) {

	}

	public String test() {
		String result = "";
		List<Customer> customers = getCustomers();
		if (customers != null) {
			for (Customer customer : customers) {
				result += "client " + customer.getName() + " \n";

			}
		} else {
			result += "Aucun client\n";
		}

		return result;
	}

	public void createEjbDao() {
		System.out.println("creating EjbDao");

		System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		System.setProperty(Context.PROVIDER_URL, "localhost:1099");
		System.setProperty(Context.URL_PKG_PREFIXES,
				"org.jboss.naming:org.jnp.interfaces");

		try {
			customerDao = (CustomerDao) new InitialContext()
					.lookup("CustomerDaoImpl/remote");// tpejb4cEAR/BankDaoImpl/remote
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	public void createLocalDao() {
		System.out.println("creating LocalDao");
		//customerDao = new CustomerDaoImpl();
	}

	public CustomerControler() {
		System.out.println("creating CustomerControler");
		//if (Bank.IS_EJB)
			createEjbDao();
	//	else
			//createLocalDao();
	}

	public CustomerDao getCustomerDao() {
		return customerDao;
	}

	public void setSession(Session session) {
		this.session = session; 
	}

	public Session getSession() {
		return session;
	}

}
